package co.com.javeriana.aes.oms.ejb.user.service;

import co.com.javeriana.aes.oms.common.entity.Address;
import co.com.javeriana.aes.oms.common.entity.Customer;
import co.com.javeriana.aes.oms.ejb.user.dto.User;
import co.com.javeriana.aes.oms.ejb.user.dto.UserAddress;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
@Local(UserServiceBeanLocal.class)
@Stateless(name = "UserServiceBean", mappedName = "ejb/UserServiceBean")
public class UserServiceBean implements UserServiceBeanLocal {

    @PersistenceContext(unitName = "OMS-PU")
    private EntityManager em;

    /**
     *
     * @param customerID
     * @return
     * @throws Exception
     */
    @Override
    public Customer getCustomer(int customerID) throws Exception {
        Customer customer = null;

        try {
            customer = em.find(Customer.class, customerID);
        } catch (Exception e) {
            throw e;
        }
        return customer;
    }
    
    
    /**
     * 
     * @param email
     * @return
     * @throws Exception 
     */
    @Override
    public Customer getCustomerByEmail(String email) throws Exception {
        
        Customer customer = null;
        
        try {
            Query queryData = em.createNamedQuery("Customer.findByEmail");
            queryData.setParameter("email", email);
            customer = (Customer) queryData.getSingleResult();
        } catch (Exception e) {
            throw e;
        }
        return customer;
    }
    
    @Override
    public Integer createCustomer(User user) throws Exception {
        
        Customer customer = null;
        
        Integer ret = 0;
        
        try {
             
            customer = new Customer();

            customer.setCreditcardnumber(user.getCreditCardNumber());
            customer.setCreditcardtype(user.getCreditCardType());
            customer.setEmail(user.getEmail());
            customer.setFname(user.getFirstName());
            customer.setLname(user.getLastName());
            customer.setPassword(user.getPassword());
            customer.setPhonenumber(user.getPhoneNumber());
            customer.setStatus("activo");
            customer.setType(user.getType().name());

            List<Address> addressEntityList = new ArrayList<Address>();

            if (user.getAddresses() != null) {

                for (UserAddress add : user.getAddresses()) {
                    Address entity = new Address();
                    //entity.setAddressid(add.getAddressid());
                    entity.setAddresstype(add.getAddresstype());
                    entity.setCity(add.getCity());
                    entity.setCountry(add.getCountry());
                    entity.setState(add.getState());
                    entity.setStreet(add.getStreet());
                    entity.setZip(add.getZip());
                    addressEntityList.add(entity);
                }

                customer.setAddressList(addressEntityList);
            }
            em.persist(customer);
            em.flush();
            ret = customer.getCusid();
        } catch (Exception e) {            
            throw e;
        }
        return ret;
    }

    /**
     *
     * @param email
     * @param password
     * @return
     * @throws Exception
     */
    @Override
    public boolean validateUser(String email, String password) throws Exception {
        boolean validate = false;
        try {

            Query queryData = em.createNamedQuery("Customer.findByEmail");
            queryData.setParameter("email", email);
            Customer c = (Customer) queryData.getSingleResult();

            if (c.getPassword().equals(password)) {
                validate = true;
            }
        } catch (Exception e) {
            throw e;
        }
        return validate;
    }

}
